"""
神经网络工具类
author:高建设
date:20180818
"""
from pandas import DataFrame
from pandas import concat


class NeuralNetworkTools:
    def __init__(self) -> None:
        super().__init__()

    # convert series to supervised learning
    def series_to_supervised(self, n_data=None, n_in=1, n_out=1, dropnan=True):
        n_vars = 1 if type(n_data) is list else n_data.shape[1]
        df = DataFrame(n_data)
        cols, names = list(), list()
        # input sequence (t-n, ... t-1)
        for i in range(n_in, 0, -1):
            cols.append(df.shift(i))
            names += [('var%d(t-%d)' % (j + 1, i)) for j in range(n_vars)]
        # forecast sequence (t, t+1, ... t+n)
        for i in range(0, n_out):
            cols.append(df.shift(-i))
            if i == 0:
                names += [('var%d(t)' % (j + 1)) for j in range(n_vars)]
            else:
                names += [('var%d(t+%d)' % (j + 1, i)) for j in range(n_vars)]
        # put it all together
        agg = concat(cols, axis=1)
        agg.columns = names
        # drop rows with NaN values
        if dropnan:
            agg.dropna(inplace=True)
        return agg

    # 填充缺失值
    def fillNaN(self, df):
        for column in df.columns:
            mean_val = df[column].mean()
            mean_val = round(mean_val, 3)
            df[column].fillna(mean_val, inplace=True)
